package com.whfc.iot.mach.codec;

import com.whfc.iot.mach.entity.MachMsg0x8B;
import io.netty.buffer.ByteBuf;

import java.math.BigDecimal;

/**
 * @Description: 下行-硬件开关机指令
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/17 9:32
 */
public class MachMsgDataCodec0x8B implements MachMsgDataCodec<MachMsg0x8B> {

    @Override
    public void decode(ByteBuf buf, MachMsg0x8B msg) {
        int subCmd = buf.readUnsignedByte();
        int speed = buf.readUnsignedShort();
        msg.setSubCmd(subCmd);
        msg.setSpeed(speed / 2000D);
    }

    @Override
    public void encode(ByteBuf buf, MachMsg0x8B msg) {
        int speed = new BigDecimal(msg.getSpeed() * 2000).intValue();
        buf.writeByte(msg.getSubCmd());
        buf.writeShort(speed);
    }
}
